var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        list: [{
            img: '',
            name: '招商银行',
            number: '221213651315313513',
            checked: 'true',
            active: "active"
        }, ],
        img_url: c.img_url,
    },
    onLoad: function() {
        this.getData();
    },
    onShow:function(){
        this.getData();
    },
    getData: function(e) {
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/userbank/lists",
            token: token,
            type: 0,
        };
        utils.request(data).then(function(res) {
            if (res.code == 0) {
                var data = res.data;
                data.forEach((item, index) => {
                    if (item.is_default == 1){
                        data[index]['checked'] = 'true';
                        data[index]['active'] = 'active';
                    }
                })
                that.setData({
                    list: data
                });
            } else {

            }
        });

    },
    delCard: function(e) {
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '确认删除银行卡？',
            success: function(res) {
                if (res.confirm) {
                    
                    var id = e.currentTarget.dataset.id;
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/wsgoods/userbank/del",
                        token: token,
                        id: id,
                    };
                    utils.request(data).then(function(res) {
                        if (res.code == 0) {
                            utils.sucMsg("删除成功");
                            that.getData();
                        } else {
                            utils.errMsg("删除失败");
                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    },
    setDefault: function (e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/userbank/default",
            token: token,
            id: id,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                let k = e.currentTarget.dataset.index;
                let str = "list[" + k + "].checked";
                let active = "list[" + k + "].active";

                that.data.list.forEach((v, k) => {
                    let str = "list[" + k + "].checked";
                    let active = "list[" + k + "].active";
                    that.setData({
                        [str]: '',
                        [active]: ''
                    })
                })
                that.setData({
                    [str]: true,
                    [active]: 'active'
                })
            } else {
                
            }
        });
    }
})